package bb.lanxing.adapter.sport;

import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;

import java.util.HashMap;
import java.util.Iterator;

import bb.lanxing.R;
import bb.lanxing.databinding.LayoutLocalWorkoutItemBinding;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.Enums;
import bb.lanxing.util.WorkoutTitleUtil;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class LocalWorkoutAdapter extends BaseQuickAdapter<Workout, BaseDataBindingHolder<LayoutLocalWorkoutItemBinding>> {
    private Function1<? super Workout, Unit> exportCallBack;
    private HashMap<Long, String> failedValueMap;
    private HashMap<Long, Integer> progressValueMap;
    private Function1<? super Workout, Boolean> uploadCallBack;


    public LocalWorkoutAdapter() {
        super(R.layout.layout_local_workout_item, null);

        this.exportCallBack = new Function1<Workout, Unit>() {

            public void invoke2(Workout it2) {

            }


            @Override
            public Unit invoke(Workout workout) {
                invoke2(workout);
                return Unit.INSTANCE;
            }
        };
        this.uploadCallBack = (Function1<Workout, Boolean>) it2 -> false;

        this.progressValueMap = new HashMap<>();
        this.failedValueMap = new HashMap<>();
    }

    public final Function1<Workout, Unit> getExportCallBack() {
        return (Function1<Workout, Unit>) this.exportCallBack;
    }

    public final void setExportCallBack(Function1<? super Workout, Unit> function1) {

        this.exportCallBack = function1;
    }

    public final Function1<Workout, Boolean> getUploadCallBack() {
        return (Function1<Workout, Boolean>) this.uploadCallBack;
    }

    public final void setUploadCallBack(Function1<? super Workout, Boolean> function1) {
        this.uploadCallBack = function1;
    }

    public final HashMap<Long, Integer> getProgressValueMap() {
        return this.progressValueMap;
    }

    public final HashMap<Long, String> getFailedValueMap() {
        return this.failedValueMap;
    }

    @Override
    public void convert(@NonNull BaseDataBindingHolder<LayoutLocalWorkoutItemBinding> holder, final Workout item) {
        String title;
        String vDetals;

        int i = 0;
//        if (item.getFitPath() != null) {
//            File file = new File(item.getFitPath());
//            str = file.getName();
//            str2 = SizeFormatter.formatFileSize(file.length());
//        } else {

            title = item.getTitle();
            if (title == null || title.isEmpty()) {
                title = WorkoutTitleUtil.buildWorkoutDefaultTitle(getContext(), item);
            }


            String distance = CommonUtil.getFormatDistance(item.getDistance());
//            vDetals = "- -";
            vDetals = distance + "Km" + " ";
            if (item.getWorkStatus() == Workout.STATUS_WORKING) {
                vDetals += "，";
            } else if (item.getWorkStatus() == Workout.STATUS_STOPED) {
                vDetals += "。";
            }
//        }
        LayoutLocalWorkoutItemBinding dataBinding = holder.getDataBinding();
        if (dataBinding == null) {
            return;
        }

        dataBinding.tvName.setText(title);
        dataBinding.tvSize.setText(vDetals);

        //not upload fit file
        dataBinding.tvExport.setVisibility(View.GONE);
//        String fitPath = item.getFitPath();
//        if (fitPath == null || fitPath.isEmpty()) {
//            dataBinding.tvExport.setVisibility(View.GONE);
//        } else {
//            dataBinding.tvExport.setVisibility(View.VISIBLE);
//            dataBinding.tvExport.setOnClickListener(view -> LocalWorkoutAdapter.this.exportCallBack.invoke(item));
//        }

        dataBinding.tvUpload.setOnClickListener(view -> {
            if (LocalWorkoutAdapter.this.uploadCallBack.invoke(item)) {
                item.setUploadStatus(Enums.UploadStatus.Uploading);
                LocalWorkoutAdapter.this.notifyItemChanged(LocalWorkoutAdapter.this.getItemPosition(item));
            }
        });

        Enums.UploadStatus uploadStatus = item.getUploadStatus();
        if (uploadStatus == null) {
            uploadStatus = Enums.UploadStatus.Uploaded;
        }

        if (uploadStatus == Enums.UploadStatus.NotUpload) {
            dataBinding.tvUpload.setVisibility(View.VISIBLE);
            dataBinding.tvUpload.setEnabled(true);
            dataBinding.tvUpload.setText(R.string.upload);
            dataBinding.progress.setVisibility(View.GONE);

            if (!this.failedValueMap.containsKey(item.getId())) {
                return;
            }
            dataBinding.llReason.setVisibility(View.VISIBLE);
            dataBinding.tvReason.setText(this.failedValueMap.get(item.getId()));
        } else if (uploadStatus != Enums.UploadStatus.Uploading) {
            if (uploadStatus != Enums.UploadStatus.Uploaded) {
                return;
            }
            dataBinding.tvUpload.setVisibility(View.GONE);
            dataBinding.progress.setVisibility(View.GONE);
            dataBinding.llReason.setVisibility(View.GONE);
        } else {
            dataBinding.tvUpload.setVisibility(View.VISIBLE);
            dataBinding.tvUpload.setEnabled(false);
            dataBinding.tvUpload.setText(R.string.st_waiting);
            dataBinding.progress.setVisibility(View.VISIBLE);

            Integer num = this.progressValueMap.get(item.getId());
            if (num != null) {
                i = num;
            }
            dataBinding.progress.setProgress(i);
            dataBinding.llReason.setVisibility(View.GONE);
        }
    }

    public final Workout getItemByWorkoutId(long wokoutId) {
        Object obj;
        boolean found;
        Iterator<Workout> it = getData().iterator();
        while (true) {
            if (!it.hasNext()) {
                obj = null;
                break;
            }
            obj = it.next();
            Long id = ((Workout) obj).getId();
            if (id != null && id == wokoutId) {
                found = true;
//                continue;
            } else {
                found = false;
//                continue;
            }
            if (found) {
                break;
            }
        }
        return (Workout) obj;
    }

    public final void setUploading(long workoutId, int i) {
        Workout itemByWorkoutId = getItemByWorkoutId(workoutId);
        if (itemByWorkoutId != null) {
            this.progressValueMap.put(workoutId, i);
            itemByWorkoutId.setUploadStatus(Enums.UploadStatus.Uploading);
            notifyItemChanged(getItemPosition(itemByWorkoutId));
        }
    }

    public final void setUploaded(long workoutId) {
        Workout itemByWorkoutId = getItemByWorkoutId(workoutId);
        if (itemByWorkoutId != null) {
            itemByWorkoutId.setUploadStatus(Enums.UploadStatus.Uploaded);
            if (this.failedValueMap.containsKey(workoutId)) {
                this.failedValueMap.remove(workoutId);
            }
            notifyItemChanged(getItemPosition(itemByWorkoutId));
        }
    }

    public final void setUploadFailed(long workoutId, String cause) {
        Workout itemByWorkoutId = getItemByWorkoutId(workoutId);
        if (itemByWorkoutId != null) {
            itemByWorkoutId.setUploadStatus(Enums.UploadStatus.NotUpload);
            this.failedValueMap.put(workoutId, cause);
            notifyItemChanged(getItemPosition(itemByWorkoutId));
        }
    }
}